package com.ymatou.iapi.productmanager.testcase;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.parameter.productCheckStatus.CheckStatusDto;
import com.ymatou.iapi.productmanager.parameter.productCheckStatus.UpdateProductCheckStatusRequest;
import com.ymatou.iapi.productmanager.service.UpdateProductCheckStatusCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.productdata.impl.ProductFactory;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.database.sqlwapper.YmtLiveWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsInLiveWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

public class Ts_UpdateProductCheckStatus {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static IntergratedProductMapper intergratedProductMapper = SqlMapperFactory.getIntergratedProductMapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新商品的审核状态");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new UpdateProductCheckStatusCall().getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductCheckStatus_001() {
		Logger.start(true, "上架商品 强制下架");
		try{
			String productId1 =  ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();
			String productId2 =  ProductFactory.getOnlyProduct(3383, "wesper").getProductId();	
			
			
			//强制下架
			UpdateProductCheckStatusRequest requestBean = getUpdateProductCheckStatusRequest(MapUtil.hashMap(sellerId,productId1,
																					3383,productId2),-1);
			
			
			JSONObject ret = new UpdateProductCheckStatusCall().callAndGetReturnData(requestBean);
			
			checkUpdateProductCheckStatus(requestBean,ret);
		}catch(Exception e){
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductCheckStatus_002() {
		Logger.start(true, "上架商品 强制下架后,强制上架");
		try{
			String productId1 =  ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();
			String productId2 =  ProductFactory.getOnlyProduct(3383, "wesper").getProductId();	
			
			
			//强制下架
			UpdateProductCheckStatusRequest requestBean = getUpdateProductCheckStatusRequest(MapUtil.hashMap(sellerId,productId1,
																					3383,productId2),-1);
			
			
			JSONObject ret = new UpdateProductCheckStatusCall().callAndGetReturnData(requestBean);
			
			checkUpdateProductCheckStatus(requestBean,ret);
			
			
			//恢复
			requestBean.setCheckStatus(0);
			JSONObject ret2 = new UpdateProductCheckStatusCall().callAndGetReturnData(requestBean);
			YMTDateUtil.waitTime(5);
			checkUpdateProductCheckStatus(requestBean,ret2);
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductCheckStatus_003() {
		Logger.start(true, "强制非该买手商品下架");
		try{
			String productId =  ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();
			
			
			//强制下架
			UpdateProductCheckStatusRequest requestBean = getUpdateProductCheckStatusRequest(MapUtil.hashMap(sellerId+1,productId),-1);
			
			
			JSONObject ret = new UpdateProductCheckStatusCall().callAndGetReturnData(requestBean);
			Logger.verifyEquals("ok", ret.getString("response"), "返回值检查");
		
			Map<String,Object> retInSql = intergratedProductMapper.selectProductByProdId(productId);
			Logger.verifyEquals(0, Integer.parseInt(retInSql.get("CheckStatus").toString()), "CheckStatus检查");
		
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductCheckStatus_004() {
		Logger.start(true, "强制下架不存在商品");
		try{
			String productId =  "123123213";
			
			//强制下架
			UpdateProductCheckStatusRequest requestBean = getUpdateProductCheckStatusRequest(MapUtil.hashMap(sellerId,productId),-1);
			
			
			JSONObject ret = new UpdateProductCheckStatusCall().callAndGetReturnData(requestBean);
			Logger.verifyEquals("ok", ret.getString("response"), "返回值检查");
		
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	public static void checkUpdateProductCheckStatus(UpdateProductCheckStatusRequest requestBean,JSONObject ret) throws Exception{
		Logger.verifyEquals("ok", ret.getString("response"), "返回值检查");
		
		List<CheckStatusDto> productList = requestBean.getProductList();
		//强制下架检查
		if (requestBean.getCheckStatus()==-1){
			for(CheckStatusDto ele: productList){
				Map<String,Object> retInSql = intergratedProductMapper.selectProductByProdId(ele.getProductId());
				Logger.verifyEquals(requestBean.getOperator(), retInSql.get("Checker"), "操作人检查");
				Logger.verifyEquals(requestBean.getCheckStatus(), Integer.parseInt(retInSql.get("CheckStatus").toString()), "CheckStatus检查");
				checkOffSaleTime(productList.stream().map(x->x.getProductId()).collect(Collectors.toList()));
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(ele.getProductId());
				
			}
		}else{
			//取消强制下架检查
			for(CheckStatusDto ele: productList){
				Map<String,Object> retInSql = intergratedProductMapper.selectProductByProdId(ele.getProductId());
				Logger.verifyEquals(requestBean.getOperator(), retInSql.get("Checker"), "操作人检查");
				Logger.verifyEquals(requestBean.getCheckStatus(), Integer.parseInt(retInSql.get("CheckStatus").toString()), "CheckStatus检查");
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(ele.getProductId());
			}
		}
		
	}
	
	public static UpdateProductCheckStatusRequest getUpdateProductCheckStatusRequest(Map<Integer,String> userIdAndProdIdList,int status){
		UpdateProductCheckStatusRequest requestBean = new UpdateProductCheckStatusRequest();
		requestBean.setOperator("autotest");
		requestBean.setCheckStatus(status);
		
		List<CheckStatusDto> productList = new ArrayList<>();
		userIdAndProdIdList.forEach((userId,prodId)->{
			CheckStatusDto cs = new CheckStatusDto();
			cs.setSellerId(userId);
			cs.setProductId(prodId);
			productList.add(cs);
			
		});
		
		requestBean.setProductList(productList);
		return requestBean;
	}
	
	
	public static void checkOffSaleTime(List<String> productIdList) throws Exception{
		Map<String,Boolean> productIdAndIsInLive = MapUtil.hashMap();
		productIdList.forEach(x->productIdAndIsInLive.put(x, false));
		checkOffSaleTime(productIdAndIsInLive);
	}
		
		
	
	@SuppressWarnings("rawtypes")
	public static void checkOffSaleTime(Map<String,Boolean> productIdAndIsInLive) throws Exception{
		Date nowDate = YMTDateUtil.getDateBeforeOrNextSecond(600);
		productIdAndIsInLive.forEach((productId,isInLive)->{
			try {
			//SQL YmtProducts
				List<Map> productIDsqllist = new YmtProductsIWapper().selectProductByProductIdforMap(productId);
				Date svalidStartbysql = (Date)productIDsqllist.get(0).get("validStart")	;
				Date svalidEndbysql = (Date)productIDsqllist.get(0).get("validEnd");	
				Logger.verifyEquals(true, svalidStartbysql.before(nowDate), "开始时间早于当前时间");
				Logger.verifyEquals(true, svalidEndbysql.before(nowDate), "结束时间早于当前时间");
				
				if (isInLive){
					List<Map> liveProductMapList = new YmtProductsInLiveWapper().selectInLiveByProductId(productId);
					liveProductMapList.forEach(x->{
						int liveId = Integer.parseInt(x.get("iActivityId").toString());
						
						Logger.verifyEquals(-1, Integer.parseInt(x.get("iAction").toString()), "下架商品，iaction应为-1 liveId:"+ liveId
									+", productId:" + x.get("sProductId").toString());
						try {
							//默认下架后，直播中没有有效商品
							Map liveMap = new YmtLiveWapper().selectforMapByLiveId(liveId).get(0);
							Logger.verifyEquals(0, Integer.parseInt(liveMap.get("ProductCount").toString()), "下架后, productcount应为0,liveId:"+ liveId);
							Logger.verifyEquals(0, Integer.parseInt(liveMap.get("PutawayCount").toString()), "下架后, PutawayCount应为0,liveId:"+ liveId);
							
							NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(liveId,false);
						} catch (Exception e) {
							e.printStackTrace();
						}
					});
				}	
			} catch (Exception e) {
				e.printStackTrace();
			}
				
		});
	}
	
	
	
	
	
}
